package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import com.itheima.service.SetmealService;
import com.itheima.utils.DateUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    private MemberService memberService;
    @Reference
    private SetmealService setmealService;

    @Reference
    private ReportService reportService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(){
        /**
         * 思路 先查询月份 这个不需要查询数据库
         * 然后创建一个map 将所有的月份放在一个list中 作为一个value存入 相对应的key为time
         * 从数据库中查询每一个时间段的数据 然后查询出一个集合 作为value存入 相对应的key为count
         * 然后把map放入Result传回前台
         */
        Map<String,Object> map = new HashMap<>();

        //查询时间
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        List<String> list = new ArrayList();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            String s = null;
            try {
                s = DateUtils.parseDate2String(calendar.getTime());
            } catch (Exception e) {
                e.printStackTrace();
            }
            list.add(s);
        }
        map.put("months",list);

        //查询所有时间段的会员数量
        List<Integer> counts = memberService.findMemberCountByMonth(list);
        map.put("memberCount",counts);
        return new Result(true, MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS,map);
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport(){
        try {
            Map<String,Object> mapAll = new HashMap<>();
            List<String> nameList = new ArrayList<>();
            List<Map<String,Object>> list = setmealService.findSetmealCount();
            for (Map<String, Object> map : list) {
                nameList.add((String) map.get("name"));
            }
            mapAll.put("names",nameList);
            mapAll.put("total",list);
            return new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,mapAll);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }

    @RequestMapping("/getBusinessReportData")
    public Result getBusinessReportData(){
        try {
            Map<String,Object> map = reportService.getBusinessReportData();
            return new Result(true,MessageConstant.GET_BUSINESS_REPORT_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }

    public static void main(String[] args) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,-12);
        List list = new ArrayList();
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            String s = DateUtils.parseDate2String(calendar.getTime());
            list.add(s);
        }
        System.out.println(list.toString());
    }
}
